﻿
using System.Text.RegularExpressions;
using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;
namespace ProCodes.BugTracker.BugCatcher
{
    public class MailOption
    {
        public static bool ValidateEmail(string sEmail)
        {

            Regex exp = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            Match m = exp.Match(sEmail);

            if (m.Success && m.Value.Equals(sEmail)) return true;

            else return false;

        }


        public enum Periority : int
        {
            Normal = 1,
            Medium,
            High
        }

        public enum ContactReceivingType : int
        {
            To = 0,
            Cc,
            BCc
        }

        public enum EmailType : int
        {
            Invitation = 1,
            Mandate
        }

        public static void SendEmail(
            string subject,
            string body,
            string[] reciptorsTo,
            string[] reciptorsCc,
            string[] reciptorsBCc,
            string[] attachedFiles)
        {

            //Create the mail message 
            MailMessage mail = new MailMessage();
            mail.Subject = subject;
            mail.Body = body;

            //the displayed "from" email address 
            mail.From = new System.Net.Mail.MailAddress(Properties.Settings.Default.EmailSender);
            mail.IsBodyHtml = false;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;

            //Add one or more addresses that will receive the mail 
            //string[] emailTo = txtTo.Text.Split(new Char[] { ';', ',', ':' });
            for (int i = 0; i < reciptorsTo.Length; i++)
            {
                if (ValidateEmail(reciptorsTo[i].ToString().Trim()))
                {
                    mail.To.Add(reciptorsTo[i].ToString().Trim());
                }
                else
                {
                    //No action
                }
            }

            //Add one or more addresses that will receive the mail as Cc
            //string[] emailCc = txtCc.Text.Split(new Char[] { ';', ',', ':' });
            for (int i = 0; i < reciptorsCc.Length; i++)
            {
                if (ValidateEmail(reciptorsCc[i].ToString().Trim()))
                {
                    mail.CC.Add(reciptorsCc[i].ToString().Trim());
                }
                else
                {
                    //No action
                }
            }

            //Add one or more addresses that will receive the mail as Cc
            //string[] emailCc = txtCc.Text.Split(new Char[] { ';', ',', ':' });
            for (int i = 0; i < reciptorsBCc.Length; i++)
            {
                if (ValidateEmail(reciptorsBCc[i].ToString().Trim()))
                {
                    mail.Bcc.Add(reciptorsBCc[i].ToString().Trim());
                }
                else
                {
                    //No action
                }
            }

            //Add one or more attachment
            for (int i = 0; i < attachedFiles.Length; i++)
            {
                string filePath = attachedFiles[i].ToString();
                mail.Attachments.Add(new Attachment(filePath));
            }

            NetworkCredential cred = new NetworkCredential(
            //from email address of the sending account 
            Properties.Settings.Default.EmailSender, 
            //password of the sending account 
            Properties.Settings.Default.EmailSenderPassword); 

            //create the smtp client...these settings are for gmail 
            SmtpClient smtp = new SmtpClient(Properties.Settings.Default.SMTP);
            smtp.UseDefaultCredentials = false;
            //smtp.EnableSsl = true;

            //credentials (username, pass of sending account) assigned here 
            smtp.Credentials = cred;
            smtp.Port = 465;

            try
            {
                if (reciptorsTo.Length > 0)
                {
                    //let her rip 
                    smtp.Send(mail);
                    MessageBox.Show(PRO_DOCS.BLL.WIN.Properties.Messages.Default.EmailSuccessfullySent, "PRO®- blue o two POS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    //No action.
                }
            }
            catch (Exception)
            {
                MessageBox.Show(PRO_DOCS.BLL.WIN.Properties.Messages.Default.EmailWasNotSent, "PRO®- blue o two POS", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
